import 'package:distribution_mall_flutter/constant/cf_constant.dart';
import 'package:distribution_mall_flutter/generated/l10n.dart';
import 'package:distribution_mall_flutter/pages/address/address_head_line.dart';
import 'package:distribution_mall_flutter/pages/address/address_search/address_search_page.dart';
import 'package:distribution_mall_flutter/pages/address/address_search/domain/address_search_entity.dart';
import 'package:distribution_mall_flutter/provider/address/address_query.dart';
import 'package:distribution_mall_flutter/utils/debug.dart';
import 'package:distribution_mall_flutter/widgets/azlistview/azlistview.dart';
import 'package:distribution_mall_flutter/widgets/bottom_sheet_appbar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class AddressAreaSelect extends ConsumerStatefulWidget {
  final Function(List<CityModel> list)? callback;

  const AddressAreaSelect({super.key, this.callback});

  @override
  createState() => _AddressAreaSelect();
}

class _AddressAreaSelect extends ConsumerState<AddressAreaSelect> {
  @override
  void initState() {
    super.initState();
    println('getQueryList2');
    WidgetsBinding.instance.addPostFrameCallback((_) {
      println('getQueryList3');
      // ref.read(addressQueryProvider.notifier).formatQueryList();
      ref.read(addressQueryProvider.notifier).initData();
    });
    // SuspensionUtil.setShowSuspensionStatus(cityList);
  }

  @override
  void dispose() {
    super.dispose();
    // ref.read(addressQueryProvider.notifier).setRegionList();
  }

  Widget getSusItem(BuildContext context, String tag) {
    return Container(
      height: 40,
      width: MediaQuery.of(context).size.width,
      padding: const EdgeInsets.only(left: EdgeConstant.edge218),
      alignment: Alignment.centerLeft,
      decoration: const BoxDecoration(
          color: Colors.white,
          border: Border(
              bottom: BorderSide(color: ColorConstant.grey233_4, width: 0.9))),
      child: Text(
        tag,
        style: const TextStyle(
          fontSize: 18.2,
          color: Colors.black,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final modal = ref.watch(addressQueryProvider);
    return Container(
      constraints:
          BoxConstraints(maxHeight: MediaQuery.of(context).size.height * 0.9),
      margin: const EdgeInsets.only(left: 24, right: 24),
      child: Column(
        children: [
          Container(
            padding: const EdgeInsets.only(top: 26, bottom: 15),
            child: BottomSheetAppbar(
              title: S.current.address_add,
            ),
          ),
          GestureDetector(
            onTap: () async {
              AddressSearchBody? selectItem = await showModalBottomSheet(
                  context: context,
                  builder: (_) => const AddressSearchPage(),
                  isScrollControlled: true,
                  enableDrag: false);
              if (selectItem != null) {
                bool? bol = ref
                    .read(addressQueryProvider.notifier)
                    .setSearchAddress(selectItem);
                if (bol == true) {
                  widget.callback!(ref.read(addressQueryProvider).regionList);
                }
              }
            },
            child: Container(
              margin: const EdgeInsets.only(bottom: 16),
              padding: const EdgeInsets.only(left: 11),
              height: 30,
              decoration: BoxDecoration(
                color: const Color(0xFFE6E6EA),
                borderRadius: BorderRadius.circular(24),
              ),
              child: Row(
                children: [
                  const Icon(
                    Icons.search,
                    color: Color(0xfF868D96),
                    size: 20,
                  ),
                  Container(
                    margin: const EdgeInsets.only(left: 9),
                    child: Text(
                      S.current.select_region_hint,
                      style: const TextStyle(
                          fontSize: 14, color: Color(0xFF868D96)),
                    ),
                  )
                ],
              ),
            ),
          ),
          Container(
              margin: const EdgeInsets.only(bottom: 16),
              child: const Divider(height: 1)),
          if (modal.regionActive != -1)
            Container(
              margin: const EdgeInsets.only(bottom: 24),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    S.current.selected_area,
                    style: const TextStyle(color: Color(0xFF868D96)),
                  ),
                  GestureDetector(
                    onTap: () {
                      ref.read(addressQueryProvider.notifier).initData();
                    },
                    child: Text(
                      S.current.reset,
                      style: const TextStyle(color: Color(0xFF868D96)),
                    ),
                  ),
                ],
              ),
            ),
          Container(
            child: AddressHeadLine(
              activeIndex: modal.regionActive,
              onTap: (item, index) {
                ref.read(addressQueryProvider.notifier).titleClickRegionList(
                      item: item,
                      index: index,
                    );
              },
              data: modal.regionList,
            ),
          ),
          Expanded(
              child: Container(
            margin: const EdgeInsets.only(top: 15),
            child: AzListView(
              data: modal.azList,
              itemCount: modal.azList.length,
              itemBuilder: (BuildContext context, int index) {
                return ListTile(
                  contentPadding: EdgeInsets.zero,
                  title: Text(
                    modal.azList[index].name,
                    style: const TextStyle(
                        color: Color.fromRGBO(119, 119, 119, 1),
                        fontSize: FontConstant.f146,
                        letterSpacing: TextConstant.letterSpacing),
                  ),
                  onTap: () {
                    bool? bol = ref
                        .read(addressQueryProvider.notifier)
                        .clickRegionList(item: modal.azList[index]);
                    if (bol == true) {
                      widget
                          .callback!(ref.read(addressQueryProvider).regionList);
                    }
                  },
                );
              },
              padding: EdgeInsets.zero,
              susItemBuilder: (BuildContext context, int index) {
                return getSusItem(
                  context,
                  modal.azList[index].tagIndex ?? '',
                );
              },
              indexBarHeight: MediaQuery.of(context).size.height * 0.6,
              indexBarData: modal.indexBarList,
            ),
          )),
        ],
      ),
    );
  }
}
